package com.morizhang.merchanmanager.api.plugins.impl;

import com.alibaba.fastjson.JSONArray;
import com.morizhang.merchanmanager.api.bean.StatisticDataHolder;
import com.morizhang.merchanmanager.api.dao.CommodityStorageDao;
import com.morizhang.merchanmanager.api.dao.PriceInfoDao;
import com.morizhang.merchanmanager.api.dao.ShopShelfInfoDao;
import com.morizhang.merchanmanager.api.dao.WarehouseShelfInfoDao;
import com.morizhang.merchanmanager.api.model.PriceModel;
import com.morizhang.merchanmanager.api.model.StockModel;
import com.morizhang.merchanmanager.api.model.statistics.IndexInfoModel;
import com.morizhang.merchanmanager.api.plugins.BasicStatistics;
import com.morizhang.merchanmanager.api.plugins.PluginAnnotation;
import org.apache.http.cookie.SM;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


@PluginAnnotation(name = "BasicStatisticsImpl")
public class BasicStatisticsImpl implements BasicStatistics {
    @Autowired
    private PriceInfoDao priceInfoDao;

    @Autowired
    private CommodityStorageDao commodityStorageDao;

    @Autowired
    private WarehouseShelfInfoDao warehouseShelfInfoDao;

    @Autowired
    private ShopShelfInfoDao shopShelfInfoDao;

    @Override
    public void updateStockStatisticsByStockList(JSONArray stockList) {

    }

    @Override
    public void updateSaleStatisticsBySaleList(JSONArray saleList) {

    }

    @Override
    public IndexInfoModel queryIndexInfo() {
        IndexInfoModel result = new IndexInfoModel();
        int storageSumNum = 0;
        float storageValue = 0;
        //查询数据缓存
        if (StatisticDataHolder.checkExpire("storageValue") && StatisticDataHolder.checkExpire("storageSumNum")) {
            Object storageValueObject = StatisticDataHolder.getData("storageValue");
            if (storageValueObject != null)
                storageValue = (float) storageValueObject;
            Object storageSumNumObject = StatisticDataHolder.getData("storageSumNum");
            if (storageSumNumObject != null)
                storageSumNum = (int) storageSumNumObject;
        } else {
            //缓存过期或不存在
            List<StockModel> allCommodityStorage = commodityStorageDao.selectAllCommodityStorage();
            for (StockModel singleStorageRecord : allCommodityStorage) {
                String batchNo = singleStorageRecord.getBatchNo();
                String barCode = singleStorageRecord.getBarCode();
                PriceModel queryParameter = new PriceModel();
                queryParameter.setBarCode(barCode);
                queryParameter.setBatchNo(batchNo);
                PriceModel queryPrice = priceInfoDao.queryCommodityStockPriceByBarcodeBatchNo(queryParameter);
                float stockPrice = queryPrice.getStockPrice();
                int amount = singleStorageRecord.getAmountNow();
                storageSumNum += amount;
                float priceOfThisRecord = stockPrice * amount;
                storageValue += priceOfThisRecord;
            }
            StatisticDataHolder.setNewData("storageValue", storageValue);
            StatisticDataHolder.setNewData("storageSumNum", storageSumNum);
        }
        result.setStorageValue(storageValue);
        result.setStorageSumNum(storageSumNum);
        result.setWarehouseNum(warehouseShelfInfoDao.queryWarehouseNum());
        result.setShopNum(shopShelfInfoDao.queryShopNum());

        return result;
    }
}
